/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.triumphsys.icici.cmeeting.portlet.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.triumphsys.icici.cmeeting.portlet.NoSuchCMParameterException;
import com.triumphsys.icici.cmeeting.portlet.model.CMParameter;
import com.triumphsys.icici.cmeeting.portlet.model.impl.CMParameterImpl;
import com.triumphsys.icici.cmeeting.portlet.model.impl.CMParameterModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the c m parameter service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link CMParameterUtil} to access the c m parameter persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Triumphsys
 * @see CMParameterPersistence
 * @see CMParameterUtil
 * @generated
 */
public class CMParameterPersistenceImpl extends BasePersistenceImpl<CMParameter>
	implements CMParameterPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = CMParameterImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(CMParameterModelImpl.ENTITY_CACHE_ENABLED,
			CMParameterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CMParameterModelImpl.ENTITY_CACHE_ENABLED,
			CMParameterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the c m parameter in the entity cache if it is enabled.
	 *
	 * @param cmParameter the c m parameter to cache
	 */
	public void cacheResult(CMParameter cmParameter) {
		EntityCacheUtil.putResult(CMParameterModelImpl.ENTITY_CACHE_ENABLED,
			CMParameterImpl.class, cmParameter.getPrimaryKey(), cmParameter);
	}

	/**
	 * Caches the c m parameters in the entity cache if it is enabled.
	 *
	 * @param cmParameters the c m parameters to cache
	 */
	public void cacheResult(List<CMParameter> cmParameters) {
		for (CMParameter cmParameter : cmParameters) {
			if (EntityCacheUtil.getResult(
						CMParameterModelImpl.ENTITY_CACHE_ENABLED,
						CMParameterImpl.class, cmParameter.getPrimaryKey(), this) == null) {
				cacheResult(cmParameter);
			}
		}
	}

	/**
	 * Clears the cache for all c m parameters.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(CMParameterImpl.class.getName());
		EntityCacheUtil.clearCache(CMParameterImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the c m parameter.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(CMParameter cmParameter) {
		EntityCacheUtil.removeResult(CMParameterModelImpl.ENTITY_CACHE_ENABLED,
			CMParameterImpl.class, cmParameter.getPrimaryKey());
	}

	/**
	 * Creates a new c m parameter with the primary key. Does not add the c m parameter to the database.
	 *
	 * @param parameterId the primary key for the new c m parameter
	 * @return the new c m parameter
	 */
	public CMParameter create(long parameterId) {
		CMParameter cmParameter = new CMParameterImpl();

		cmParameter.setNew(true);
		cmParameter.setPrimaryKey(parameterId);

		return cmParameter;
	}

	/**
	 * Removes the c m parameter with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the c m parameter to remove
	 * @return the c m parameter that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a c m parameter with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMParameter remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the c m parameter with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param parameterId the primary key of the c m parameter to remove
	 * @return the c m parameter that was removed
	 * @throws com.triumphsys.icici.cmeeting.portlet.NoSuchCMParameterException if a c m parameter with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMParameter remove(long parameterId)
		throws NoSuchCMParameterException, SystemException {
		Session session = null;

		try {
			session = openSession();

			CMParameter cmParameter = (CMParameter)session.get(CMParameterImpl.class,
					new Long(parameterId));

			if (cmParameter == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + parameterId);
				}

				throw new NoSuchCMParameterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					parameterId);
			}

			return remove(cmParameter);
		}
		catch (NoSuchCMParameterException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected CMParameter removeImpl(CMParameter cmParameter)
		throws SystemException {
		cmParameter = toUnwrappedModel(cmParameter);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, cmParameter);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(CMParameterModelImpl.ENTITY_CACHE_ENABLED,
			CMParameterImpl.class, cmParameter.getPrimaryKey());

		return cmParameter;
	}

	public CMParameter updateImpl(
		com.triumphsys.icici.cmeeting.portlet.model.CMParameter cmParameter,
		boolean merge) throws SystemException {
		cmParameter = toUnwrappedModel(cmParameter);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, cmParameter, merge);

			cmParameter.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(CMParameterModelImpl.ENTITY_CACHE_ENABLED,
			CMParameterImpl.class, cmParameter.getPrimaryKey(), cmParameter);

		return cmParameter;
	}

	protected CMParameter toUnwrappedModel(CMParameter cmParameter) {
		if (cmParameter instanceof CMParameterImpl) {
			return cmParameter;
		}

		CMParameterImpl cmParameterImpl = new CMParameterImpl();

		cmParameterImpl.setNew(cmParameter.isNew());
		cmParameterImpl.setPrimaryKey(cmParameter.getPrimaryKey());

		cmParameterImpl.setParameterId(cmParameter.getParameterId());
		cmParameterImpl.setCompanyId(cmParameter.getCompanyId());
		cmParameterImpl.setGroupId(cmParameter.getGroupId());
		cmParameterImpl.setUserId(cmParameter.getUserId());
		cmParameterImpl.setName(cmParameter.getName());
		cmParameterImpl.setValue(cmParameter.getValue());
		cmParameterImpl.setDescription(cmParameter.getDescription());
		cmParameterImpl.setCreateDate(cmParameter.getCreateDate());
		cmParameterImpl.setModifiedBy(cmParameter.getModifiedBy());
		cmParameterImpl.setModifiedDate(cmParameter.getModifiedDate());
		cmParameterImpl.setRegionGroupId(cmParameter.getRegionGroupId());
		cmParameterImpl.setActive_(cmParameter.isActive_());

		return cmParameterImpl;
	}

	/**
	 * Finds the c m parameter with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the c m parameter to find
	 * @return the c m parameter
	 * @throws com.liferay.portal.NoSuchModelException if a c m parameter with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMParameter findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the c m parameter with the primary key or throws a {@link com.triumphsys.icici.cmeeting.portlet.NoSuchCMParameterException} if it could not be found.
	 *
	 * @param parameterId the primary key of the c m parameter to find
	 * @return the c m parameter
	 * @throws com.triumphsys.icici.cmeeting.portlet.NoSuchCMParameterException if a c m parameter with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMParameter findByPrimaryKey(long parameterId)
		throws NoSuchCMParameterException, SystemException {
		CMParameter cmParameter = fetchByPrimaryKey(parameterId);

		if (cmParameter == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + parameterId);
			}

			throw new NoSuchCMParameterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				parameterId);
		}

		return cmParameter;
	}

	/**
	 * Finds the c m parameter with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the c m parameter to find
	 * @return the c m parameter, or <code>null</code> if a c m parameter with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMParameter fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the c m parameter with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param parameterId the primary key of the c m parameter to find
	 * @return the c m parameter, or <code>null</code> if a c m parameter with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMParameter fetchByPrimaryKey(long parameterId)
		throws SystemException {
		CMParameter cmParameter = (CMParameter)EntityCacheUtil.getResult(CMParameterModelImpl.ENTITY_CACHE_ENABLED,
				CMParameterImpl.class, parameterId, this);

		if (cmParameter == null) {
			Session session = null;

			try {
				session = openSession();

				cmParameter = (CMParameter)session.get(CMParameterImpl.class,
						new Long(parameterId));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (cmParameter != null) {
					cacheResult(cmParameter);
				}

				closeSession(session);
			}
		}

		return cmParameter;
	}

	/**
	 * Finds all the c m parameters.
	 *
	 * @return the c m parameters
	 * @throws SystemException if a system exception occurred
	 */
	public List<CMParameter> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the c m parameters.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of c m parameters to return
	 * @param end the upper bound of the range of c m parameters to return (not inclusive)
	 * @return the range of c m parameters
	 * @throws SystemException if a system exception occurred
	 */
	public List<CMParameter> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the c m parameters.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of c m parameters to return
	 * @param end the upper bound of the range of c m parameters to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of c m parameters
	 * @throws SystemException if a system exception occurred
	 */
	public List<CMParameter> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<CMParameter> list = (List<CMParameter>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_CMPARAMETER);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_CMPARAMETER.concat(CMParameterModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<CMParameter>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<CMParameter>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the c m parameters from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (CMParameter cmParameter : findAll()) {
			remove(cmParameter);
		}
	}

	/**
	 * Counts all the c m parameters.
	 *
	 * @return the number of c m parameters
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_CMPARAMETER);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the c m parameter persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.triumphsys.icici.cmeeting.portlet.model.CMParameter")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<CMParameter>> listenersList = new ArrayList<ModelListener<CMParameter>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<CMParameter>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(CMParameterImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = CMHolidayPersistence.class)
	protected CMHolidayPersistence cmHolidayPersistence;
	@BeanReference(type = CMParameterPersistence.class)
	protected CMParameterPersistence cmParameterPersistence;
	@BeanReference(type = CMUserPersistence.class)
	protected CMUserPersistence cmUserPersistence;
	@BeanReference(type = CMCreditRatingPersistence.class)
	protected CMCreditRatingPersistence cmCreditRatingPersistence;
	@BeanReference(type = COEPropoalTypePersistence.class)
	protected COEPropoalTypePersistence coePropoalTypePersistence;
	@BeanReference(type = CMIndustryPersistence.class)
	protected CMIndustryPersistence cmIndustryPersistence;
	@BeanReference(type = CMCompanyPersistence.class)
	protected CMCompanyPersistence cmCompanyPersistence;
	@BeanReference(type = CMControlGroupPersistence.class)
	protected CMControlGroupPersistence cmControlGroupPersistence;
	@BeanReference(type = CMRolePersistence.class)
	protected CMRolePersistence cmRolePersistence;
	@BeanReference(type = CMBranchPersistence.class)
	protected CMBranchPersistence cmBranchPersistence;
	@BeanReference(type = CMRegionPersistence.class)
	protected CMRegionPersistence cmRegionPersistence;
	@BeanReference(type = CMForumPersistence.class)
	protected CMForumPersistence cmForumPersistence;
	@BeanReference(type = CMSubForumPersistence.class)
	protected CMSubForumPersistence cmSubForumPersistence;
	@BeanReference(type = CMSanctionProposalPersistence.class)
	protected CMSanctionProposalPersistence cmSanctionProposalPersistence;
	@BeanReference(type = CMReportingProposalPersistence.class)
	protected CMReportingProposalPersistence cmReportingProposalPersistence;
	@BeanReference(type = CMPostProposalPersistence.class)
	protected CMPostProposalPersistence cmPostProposalPersistence;
	@BeanReference(type = CMMeetingPersistence.class)
	protected CMMeetingPersistence cmMeetingPersistence;
	@BeanReference(type = CMProposalTypePersistence.class)
	protected CMProposalTypePersistence cmProposalTypePersistence;
	@BeanReference(type = CMEntityPersistence.class)
	protected CMEntityPersistence cmEntityPersistence;
	@BeanReference(type = CMOthersProposalPersistence.class)
	protected CMOthersProposalPersistence cmOthersProposalPersistence;
	@BeanReference(type = CMValuationProposalPersistence.class)
	protected CMValuationProposalPersistence cmValuationProposalPersistence;
	@BeanReference(type = CMPoliciesProposalPersistence.class)
	protected CMPoliciesProposalPersistence cmPoliciesProposalPersistence;
	@BeanReference(type = CMProposalPersistence.class)
	protected CMProposalPersistence cmProposalPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_CMPARAMETER = "SELECT cmParameter FROM CMParameter cmParameter";
	private static final String _SQL_COUNT_CMPARAMETER = "SELECT COUNT(cmParameter) FROM CMParameter cmParameter";
	private static final String _ORDER_BY_ENTITY_ALIAS = "cmParameter.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No CMParameter exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(CMParameterPersistenceImpl.class);
}